/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rest.jbpm;

import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPAttributeSet;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPSearchConstraints;
import com.novell.ldap.LDAPSearchResults;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class BMSTULDAPSearch {
    private static Logger log = LoggerFactory.getLogger(BMSTULDAPSearch.class);
    private LDAPConnection lc = new LDAPConnection();
    public static final String GRP_NAME = "dc=GRP,dc=eu,dc=bmstu,dc=ru";
    public static final String USR_NAME = "dc=USR,dc=EU,dc=BMSTU,dc=RU";

    public BMSTULDAPSearch(int port, String host, String login, String psw) {
        this( port, host, LDAPConnection.LDAP_V3, login, psw );
    }

    public BMSTULDAPSearch(int port, String host, int version, String login, String psw) {
        try {
            lc.connect(host, port);
            lc.bind(version, login, psw.getBytes("UTF8"));
        } catch (Exception ex) {
           log.error("constructor", ex);
        }
    }

    @Override
    protected void finalize (){
        try {
            lc.disconnect();
        } catch (LDAPException ex) {
           log.error("destructor", ex);
        }
    }


     public Map<String, Map> searchDynamicGroupEntry (String groupType ) {
           return searchDynamicGroupEntry(  groupType, new String[0]);
     }

    public Map<String, Map> searchDynamicGroupEntry ( String groupType,  String[] attrList ) {

        Map<String, Map> result=new HashMap<String, Map>();
        int searchScope = LDAPConnection.SCOPE_SUB;

        String searchFilter = "(&(objectClass=bmstuCGroup)(bmstuGroupType="+groupType+"))";
        LDAPSearchConstraints cons = new LDAPSearchConstraints();
        cons.setTimeLimit( 10000 ) ;
        try {
            LDAPSearchResults searchResults =
                lc.search(GRP_NAME, searchScope, searchFilter, attrList, false, cons );
            LDAPEntry nextEntry = null ;
           while (( nextEntry = searchResults.next()) != null ) {
                LDAPAttributeSet attributeSet = nextEntry.getAttributeSet();
                Iterator allAttributes = attributeSet.iterator();

                Map<String, List> att=new HashMap<String, List>();
                while ( allAttributes.hasNext() ) {
                    LDAPAttribute attribute =
                                (LDAPAttribute)allAttributes.next();
                    String attributeName = attribute.getName();
                    Enumeration allValues = attribute.getStringValues();

                    if( allValues != null) {
                        List<String> vals=new ArrayList<String>();
                        while(allValues.hasMoreElements()) {
                            String Value = (String) allValues.nextElement();
                            vals.add(Value);

                        }
                        att.put(attributeName, vals);
                    }
                }
                result.put(nextEntry.getDN(), att);
            }
        }
        catch( LDAPException e ) {  log.error("searchDynamicGroupEntry", e);   }
        return result;

    }


    public Map<String, List> getGroupById(String id) {

        String[] attrList= new String[0];
        Map<String, List> result=new HashMap<String, List>();
        int searchScope = LDAPConnection.SCOPE_SUB;

        String searchFilter ="(entryDN:dnSubtreeMatch:=" + id+")";
        LDAPSearchConstraints cons = new LDAPSearchConstraints();
        cons.setTimeLimit( 10000 ) ;
        try {
            LDAPSearchResults searchResults =
                lc.search(GRP_NAME, searchScope, searchFilter, attrList, false, cons );
            LDAPEntry nextEntry = null ;
           if (( nextEntry = searchResults.next()) != null ) {
                LDAPAttributeSet attributeSet = nextEntry.getAttributeSet();
                Iterator allAttributes = attributeSet.iterator();


                while ( allAttributes.hasNext() ) {
                    LDAPAttribute attribute =
                                (LDAPAttribute)allAttributes.next();
                    String attributeName = attribute.getName();
                    Enumeration allValues = attribute.getStringValues();

                    if( allValues != null) {
                        List<String> vals=new ArrayList<String>();
                        while(allValues.hasMoreElements()) {
                            String Value = (String) allValues.nextElement();
                            vals.add(Value);
                        }
                        result.put(attributeName, vals);
                    }
                }
            }
        }
        catch( LDAPException e ) {  log.error("getGroupById", e);      }
        return result;

     }

     public Map<String, List> getGroupByUser(String user) {

        String[] attrList= new String[0];
        Map<String, List> result=new HashMap<String, List>();
        int searchScope = LDAPConnection.SCOPE_SUB;

        String searchFilter ="(member=" + user +")";
        LDAPSearchConstraints cons = new LDAPSearchConstraints();
        cons.setTimeLimit( 10000 ) ;
        try {
            LDAPSearchResults searchResults =
                lc.search(GRP_NAME, searchScope, searchFilter, attrList, false, cons );
            LDAPEntry nextEntry = null ;
           if (( nextEntry = searchResults.next()) != null ) {
                LDAPAttributeSet attributeSet = nextEntry.getAttributeSet();
                Iterator allAttributes = attributeSet.iterator();


                while ( allAttributes.hasNext() ) {
                    LDAPAttribute attribute =
                                (LDAPAttribute)allAttributes.next();
                    String attributeName = attribute.getName();
                    Enumeration allValues = attribute.getStringValues();

                    if( allValues != null) {
                        List<String> vals=new ArrayList<String>();
                        while(allValues.hasMoreElements()) {
                            String Value = (String) allValues.nextElement();
                            vals.add(Value);
                        }
                        result.put(attributeName, vals);
                    }
                }
            }
        }
        catch( LDAPException e ) {  log.error("getGroupById", e);      }
        return result;

     }

    public Map<String, List> getUserById(String id) {

           String[] attrList= new String[0];
        Map<String, List> result=new HashMap<String, List>();
        int searchScope = LDAPConnection.SCOPE_SUB;

        String searchFilter ="(entryDN:dnSubtreeMatch:=" + id+")";
        LDAPSearchConstraints cons = new LDAPSearchConstraints();
        cons.setTimeLimit( 10000 ) ;
        try {
            LDAPSearchResults searchResults =
                lc.search(USR_NAME, searchScope, searchFilter, attrList, false, cons );
            LDAPEntry nextEntry = null ;
           if (( nextEntry = searchResults.next()) != null ) {
                LDAPAttributeSet attributeSet = nextEntry.getAttributeSet();
                Iterator allAttributes = attributeSet.iterator();
                while ( allAttributes.hasNext() ) {
                    LDAPAttribute attribute =
                                (LDAPAttribute)allAttributes.next();
                    String attributeName = attribute.getName();
                    Enumeration allValues = attribute.getStringValues();

                    if( allValues != null) {
                        List<String> vals=new ArrayList<String>();
                        while(allValues.hasMoreElements()) {
                            String Value = (String) allValues.nextElement();
                            vals.add(Value);
                        }
                        result.put(attributeName, vals);
                    }
                }
            }
        }
        catch( LDAPException e ) { log.error("getUserById", e);     }
        return result;
     }


}
